package com.labofjet.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.labofjet.bean.impl.EState;
import com.labofjet.exception.BusinessException;
import com.labofjet.exception.SystemException;
import com.labofjet.exception.ValidationException;
import com.labofjet.web.bean.impl.JsonContextDTO;

/**
 * 对外界屏蔽各种java异常,抛出统一的异常
 * 
 * @author jyzjyz12@163.com
 * 
 */
public abstract class BaseExceptionControllerAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseLogAspect.class);

    protected Object checkException(ProceedingJoinPoint pjp) throws Throwable {
        Object returnObj = null;
        try {
            returnObj = pjp.proceed();
        } catch (ValidationException e) {
            LOGGER.info("发现并开始处理Controller中的ValidationException");
            LOGGER.debug("ValidationException的具体信息为", e);
            JsonContextDTO dto = new JsonContextDTO();
            dto.setStateCode(EState.VALIDATION_EXCEPTION.getCode());
            dto.setException(e);
            returnObj = dto;
        } catch (BusinessException e) {
            LOGGER.info("发现并开始处理Controller中的BusinessException");
            LOGGER.debug("BusinessException的具体信息为", e);
            JsonContextDTO dto = new JsonContextDTO();
            dto.setStateCode(EState.BUSSINESS_EXCEPTION.getCode());
            dto.setException(e);
            returnObj = dto;
        } catch (Exception e) {
            LOGGER.error("发现并开始处理Controller中的意料之外的异常");
            LOGGER.error("Exception的具体信息为", e);
            JsonContextDTO dto = new JsonContextDTO();
            dto.setStateCode(EState.SYSTEM_EXCEPTION.getCode());
            SystemException se = new SystemException(EState.BUSSINESS_EXCEPTION.getText());
            dto.setException(se);
            returnObj = dto;
        }
        return returnObj;
    }
}
